using System;
using System.IO;
using System.Xml;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using WeifenLuo.WinFormsUI.Base;

namespace WeifenLuo.WinFormsUI.Security
{
	/// <summary>
	/// Summary description for Ctrl_Database.
	/// </summary>
	public class Ctrl_Database
	{
		private string	ms_ConnectionString;
		private string	ms_path = Application.StartupPath;

		public Ctrl_Database()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		// Set connection string to the connection string variable from the config file.
		public void SetConnectionString()
		{	// get ConnectionString from config file
			string ls_strXmlPath = Application.StartupPath + @"\config.xml";
			System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
			doc.Load(ls_strXmlPath);
			string ls_strConnection = doc.DocumentElement.ChildNodes[0].InnerText;
			// Set the connection string to the ConnectionString variable.
			SetConnectionString(ls_strConnection);
		}

		public void SetConnectionString(string as_ConnectionString)
		{
			ms_ConnectionString = as_ConnectionString;
		}

		public string SetConnectionString(string as_ServerName, string as_DBName)
		{	// Old connection string
			//string ls_ConnStr = @"Data Source=" + as_ServerName + ";Initial Catalog=" + as_DBName + ";Persist Security Info=False;User ID=" + as_UserName + ";Password=" + as_Password + ";Packet Size=4096";
			// New connection string for window id login
			string ls_ConnStr = @"Data Source=" + as_ServerName + ";Initial Catalog=" + as_DBName + ";packet size=4096;user id=sa;integrated security=SSPI;persist security info=False";
			ms_ConnectionString = ls_ConnStr;
			UpdateConnectionString(ls_ConnStr);
			return ls_ConnStr;
		}
		// update ConnecctionString to config.xml file
		private void UpdateConnectionString(string as_ConnStr)
		{
			try
			{
				string ls_strXmlPath = ms_path + @"\config.xml";
				if ((File.GetAttributes(ls_strXmlPath) & FileAttributes.ReadOnly)== FileAttributes.ReadOnly)
					File.SetAttributes(ls_strXmlPath, FileAttributes.Normal);

				System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
				doc.Load(ls_strXmlPath);
				XmlElement lxmlElement = (XmlElement)doc.DocumentElement.FirstChild;
				lxmlElement.InnerText = as_ConnStr;

				XmlTextWriter xmltextwriter = new XmlTextWriter(ls_strXmlPath, null);
				doc.WriteContentTo(xmltextwriter);
				xmltextwriter.Close();
				doc.RemoveAll();
			}
			catch(Exception ex)
			{
				Ctrl_LOG.Print("In Controller ", " at UpdateConnection String method", ex.Message);
			}
		}

		public string GetConnectionString()
		{
			return ms_ConnectionString;
		}
		
		// get database name from ms_ConnectionString variable
		public string GetDBName()
		{
			if (ms_ConnectionString != null && ms_ConnectionString != "")
			{
				int li_InitialCatalog = ms_ConnectionString.IndexOf("Initial Catalog=", 0),
					li_semicolon = ms_ConnectionString.IndexOf(";", li_InitialCatalog + 16);
				return ms_ConnectionString.Substring(li_InitialCatalog + 16, li_semicolon - li_InitialCatalog - 16);
			}
			return "";
		}

		// get Server name from ms_ConnectionString variable
		public string GetServerName()
		{
			if (ms_ConnectionString != null && ms_ConnectionString != "")
			{
				int li_InitialCatalog = ms_ConnectionString.IndexOf("Data Source=", 0),
					li_semicolon = ms_ConnectionString.IndexOf(";", li_InitialCatalog + 12);
				return ms_ConnectionString.Substring(li_InitialCatalog + 12, li_semicolon - li_InitialCatalog - 12);
			}
			return "";
		}

	}
}
